﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace BAL.EstRec.FDVIndicador
{
    public class ViewModelSaveCapFdv : INotifyPropertyChanged
    {
        System.Timers.Timer t;
        string _message;
        bool _jobDone;
        CollectionsPivotFDV cpfdv;
        long unidVersion;

        public string Message
        {
            get { return _message; }
            set
            {
                if (value != _message)
                {
                    this._message = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Message"));
                    }
                }
            }
        }

        public bool JobDone
        {
            get { return _jobDone; }
            set
            {
                if (value != _jobDone)
                {
                    this._jobDone = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("JobDone"));
                    }
                }
            }
        }

        public ViewModelSaveCapFdv(CollectionsPivotFDV cpfdv, long unidVersion)
        {
            this.Message = "Guardando informacion";
            this.cpfdv = cpfdv;
            this.unidVersion = unidVersion;
            this._jobDone = false;
            t = new System.Timers.Timer(100);
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(save);
        }

        public void save(Object sender, System.Timers.ElapsedEventArgs args)
        {
            this.t.Enabled = false;
            ((System.Timers.Timer)sender).Stop();
            CapturaPivotFDV.FlushCollection(this.cpfdv, this.unidVersion);
            this.Message = "Información guardada";
            this.JobDone = true;
        }

        public void start()
        {
            t.Start();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
